<template>
  <div class="main">
    <!-- 左边区域 -->
    <div class="box">
      <div class="panel">
        <div class="first">
          <span> </span>
          <h4>司机</h4>
        </div>
        <div class="driver">
          <span class="showDriver">
            <p class="number">{{ driver.driverSum }}</p>
            <p>司机总数</p>
          </span>
          <span class="showDriver">
            <p class="number">{{ driver.waitDriverSum }}</p>
            <p>待发车司机</p>
          </span>
          <span class="showDriver">
            <p class="number">{{ driver.transportDriverSum }}</p>
            <p>已发车</p>
          </span>
        </div>
      </div>
      <div class="panel">
        <div class="first">
          <span> </span>
          <h4>车辆装载</h4>
        </div>
        <div style="width: auto; height: 250px" id="car" />
        <div id="charts"></div>
        <div class="panel-footer"></div>
      </div>
      <div class="panel">
        <div class="first">
          <span> </span>
          <h4>运单排行榜</h4>
        </div>
        <div style="width: 100%; height: 500px" id="mycharts" />
        <div id="charts"></div>
        <div class="panel-footer"></div>
      </div>
    </div>
    <!-- 中间区域 -->
    <div class="box">
      <div class="on">
        <div class="first">
          <span> </span>
          <h4>运单统计</h4>
        </div>
        <div class="on_hd">
          <ul class="hd">
            <li>
              <div class="left">
                <img src="/img/await.png" alt="" />
              </div>
              <div class="right">
                <p>{{ transportInfo.taskSum }}</p>
                <span>运单总数</span>
              </div>
            </li>
            <li>
              <div class="left">
                <img src="/img/waybill.png" alt="" />
              </div>
              <div class="right">
                <p>{{ transportInfo.waitTaskSum }}</p>
                <span>待发车</span>
              </div>
            </li>
            <li>
              <div class="left">
                <img src="/img/transit.png" alt="" />
              </div>
              <div class="right">
                <p>{{ transportInfo.transportTaskSum }}</p>
                <span>已提货</span>
              </div>
            </li>
            <li>
              <div class="left">
                <img src="/img/arrive.png" alt="" />
              </div>
              <div class="right">
                <p>{{ transportInfo.finishTaskSum }}</p>
                <span>已完成</span>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="map">
        <div class="first">
          <span> </span>
          <h4>运单线路图</h4>
        </div>
        <div ref="mapBox" id="mapBox"></div>
      </div>
    </div>
    <!-- 右侧区域 -->
    <div class="box">
      <!-- 运输准时率 -->
      <div class="box1">
        <div class="first">
          <span> </span>
          <h4>运输准时率</h4>
        </div>
        <div class="xuxian">
          <img src="/img/虚线.png" alt="" />
        </div>
        <div class="time">
          <div>
            <!-- 发出 -->
            <div class="img">
              <img src="/img/depart.png" alt="" />
            </div>
            <div class="probability">
              <p>{{ punctuality.departRate }}</p>
              <p>发出准时率</p>
            </div>
          </div>
          <div>
            <!-- 到达 -->
            <div class="img">
              <img src="/img/reach.png" alt="" />
            </div>
            <div class="probability">
              <p>{{ punctuality.finishRate }}</p>
              <p>到达准时率</p>
            </div>
          </div>
        </div>
      </div>
      <!-- 异常运输 -->
      <div class="pane">
        <div class="yichang_top">
          <div class="first">
            <span> </span>
            <h4>异常运输</h4>
          </div>
          <div class="pane_top">
            <div class="yichang_left">
              <p>异常运输任务总数</p>
              <p>{{ anomaly.exceptionCount }}</p>
            </div>
            <div class="yichang_right">
              <p>异常占比</p>
              <p>{{ anomaly.exceptionRate }}%</p>
            </div>
          </div>
        </div>
        <div class="link">
          <img src="/img/gang.png" alt="" />
          <img src="/img/gang.png" alt="" />
        </div>
        <div class="yichang_bottom">
          <table class="arrive">
            <tr>
              <th>司机名称</th>
              <th>路线</th>
              <th>天数</th>
            </tr>
          </table>
        </div>
        <vue-seamless-scroll :data="anomalyMsg" class="seamless-warp2">
          <ul class="item">
            <li v-for="(item, index) in anomalyMsg" :key="index">
              <span class="title" v-text="item.driverName"></span>
              <span class="title" v-text="item.taskLine"></span>
              <span class="title" v-text="item.days"></span>
            </li>
          </ul>
        </vue-seamless-scroll>
      </div>
      <!-- 实时到达 -->
      <div class="panel">
        <div>
          <div class="first">
            <span> </span>
            <h4>实时到达</h4>
          </div>
          <table class="arrive">
            <tr>
              <th>司机名称</th>
              <th>路线</th>
              <th>完成时间</th>
            </tr>
          </table>
          <vue-seamless-scroll
            :data="punctualityMsg"
            :class-option="classOption"
            class="seamless-warp2"
          >
            <ul class="item">
              <li v-for="(item, index) in punctualityMsg" :key="index">
                <span class="title" v-text="item.driverName"></span>
                <span class="title" v-text="item.taskLine"></span>
                <span class="title" v-text="item.finishDate"></span>
              </li>
            </ul>
          </vue-seamless-scroll>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  driverMsg,
  carMsg,
  bournMsg,
  transportMsg,
  lineMsg,
  rightMsg,
} from "@/api/tms/newspaper";
require("echarts/map/js/china");
// 地图经纬度
var geoCoordMap = {
  宝应线: [119.3, 33.23],
  上海: [121.4648, 31.2891],
  东莞: [113.8953, 22.901],
  东营: [118.7073, 37.5513],
  中山: [113.4229, 22.478],
  临汾: [111.4783, 36.1615],
  临沂: [118.3118, 35.2936],
  丹东: [124.541, 40.4242],
  丽水: [119.5642, 28.1854],
  乌鲁木齐: [87.9236, 43.5883],
  佛山: [112.8955, 23.1097],
  保定: [115.0488, 39.0948],
  兰州: [103.5901, 36.3043],
  包头: [110.3467, 41.4899],
  北京: [116.4551, 40.2539],
  北海: [109.314, 21.6211],
  南京: [118.8062, 31.9208],
  南宁: [108.479, 23.1152],
  南昌: [116.0046, 28.6633],
  南通: [121.1023, 32.1625],
  厦门: [118.1689, 24.6478],
  台州: [121.1353, 28.6688],
  合肥: [117.29, 32.0581],
  呼和浩特: [111.4124, 40.4901],
  咸阳: [108.4131, 34.8706],
  哈尔滨: [127.9688, 45.368],
  唐山: [118.4766, 39.6826],
  嘉兴: [120.9155, 30.6354],
  大同: [113.7854, 39.8035],
  大连: [122.2229, 39.4409],
  天津: [117.4219, 39.4189],
  太原: [112.3352, 37.9413],
  威海: [121.9482, 37.1393],
  宁波: [121.5967, 29.6466],
  宝鸡: [107.1826, 34.3433],
  宿迁: [118.5535, 33.7775],
  常州线: [119.4543, 31.5582],
  常州: [119.4543, 31.5582],
  广州: [113.5107, 23.2196],
  廊坊: [116.521, 39.0509],
  延安: [109.1052, 36.4252],
  张家口: [115.1477, 40.8527],
  徐州: [117.5208, 34.3268],
  德州: [116.6858, 37.2107],
  惠州: [114.6204, 23.1647],
  成都: [103.9526, 30.7617],
  扬州: [119.4653, 32.8162],
  承德: [117.5757, 41.4075],
  拉萨: [91.1865, 30.1465],
  无锡: [120.3442, 31.5527],
  苏南线: [120.3442, 31.5527],
  日照: [119.2786, 35.5023],
  昆明: [102.9199, 25.4663],
  杭州: [119.5313, 29.8773],
  枣庄: [117.323, 34.8926],
  柳州: [109.3799, 24.9774],
  株洲: [113.5327, 27.0319],
  武汉: [114.3896, 30.6628],
  汕头: [117.1692, 23.3405],
  江门: [112.6318, 22.1484],
  沈阳: [123.1238, 42.1216],
  沧州: [116.8286, 38.2104],
  河源: [114.917, 23.9722],
  泉州: [118.3228, 25.1147],
  泰安: [117.0264, 36.0516],
  泰州: [120.0586, 32.5525],
  济南: [117.1582, 36.8701],
  济宁: [116.8286, 35.3375],
  海口: [110.3893, 19.8516],
  淄博: [118.0371, 36.6064],
  淮安: [118.927, 33.4039],
  深圳: [114.5435, 22.5439],
  清远: [112.9175, 24.3292],
  温州: [120.498, 27.8119],
  渭南: [109.7864, 35.0299],
  湖州: [119.8608, 30.7782],
  湘潭: [112.5439, 27.7075],
  滨州: [117.8174, 37.4963],
  潍坊: [119.0918, 36.524],
  烟台: [120.7397, 37.5128],
  玉溪: [101.9312, 23.8898],
  珠海: [113.7305, 22.1155],
  盐城: [120.2234, 33.5577],
  盘锦: [121.9482, 41.0449],
  石家庄: [114.4995, 38.1006],
  福州: [119.4543, 25.9222],
  秦皇岛: [119.2126, 40.0232],
  绍兴: [120.564, 29.7565],
  聊城: [115.9167, 36.4032],
  肇庆: [112.1265, 23.5822],
  舟山: [122.2559, 30.2234],
  苏州: [120.6519, 31.3989],
  莱芜: [117.6526, 36.2714],
  安徽: [117.283, 31.8612],
  仪征: [119.184, 32.272],
  江阴: [120.27, 31.90],
  吴江: [116.397128,39.916527],
  菏泽: [115.6201, 35.2057],
  营口: [122.4316, 40.4297],
  葫芦岛: [120.1575, 40.578],
  衡水: [115.8838, 37.7161],
  衢州: [118.6853, 28.8666],
  西宁: [101.4038, 36.8207],
  西安: [109.1162, 34.2004],
  贵阳: [106.6992, 26.7682],
  连云港: [119.1248, 34.552],
  邢台: [114.8071, 37.2821],
  邯郸: [114.4775, 36.535],
  郑州: [113.4668, 34.6234],
  鄂尔多斯: [108.9734, 39.2487],
  重庆: [107.7539, 30.1904],
  金华: [120.0037, 29.1028],
  铜川: [109.0393, 35.1947],
  银川: [106.3586, 38.1775],
  镇江: [119.4763, 31.9702],
  长春: [125.8154, 44.2584],
  长沙: [113.0823, 28.2568],
  长治: [112.8625, 36.4746],
  阳泉: [113.4778, 38.0951],
  青岛: [120.4651, 36.3373],
  韶关: [113.7964, 24.7028],
};
var planePath = "path://img/箭头.png";
export default {
  name: "echarts",
  data() {
    return {
      // 车辆装载
      carValue: [{ name: "满载" }, { name: "空载" }, { name: "报修" }],
      timer: undefined,
      driver: {},
      car: {},
      carMessage: [],
      transportInfo: {},
      bournInfo: [],
      bournX: [],
      bournY: [],
      depart: {},
      arrive: [],
      // 准时率
      punctuality: {},
      punctualityMsg: [],
      // 异常
      anomaly: {},
      anomalyMsg: [],
      // YCData: [],
      YCData: [],
      color: ["#000", "#000", "#000"],
      // 无限循环速度
      classOption: {
        step: 1,
      },
    };
  },
  mounted() {
    this.echartsInit();
    this.chinachart();
    this.getCar();
    // this.myChart();
  },
  updated() {
    // this.getCar();
  },
  methods: {
    getCar() {
      // 司机
      driverMsg().then((res) => {
        this.driver = res.data;
        if (this.driver.driverNum == null) {
          this.driver.driverNum = 0;
        }
        if (this.driver.waitDriverSum == null) {
          this.driver.waitDriverSum = 0;
        }
        if (this.driver.transportDriverSum == null) {
          this.driver.transportDriverSum = 0;
        }
      });
      carMsg().then((res) => {
        let carList = [];
        for (const i in res.data) {
          carList.push(res.data[i]);
        }
        this.carValue.map((item, index) => {
          this.carMessage.push(Object.assign(item, { value: carList[index] }));
        });
        if (res.msg) {
          this.$nextTick(() => {
            this.echartsCar();
          });
        }
      });
      // 目的地运输
      bournMsg().then((res) => {
        for (const i in res.data) {
          this.bournX.push(i);
          this.bournY.push(res.data[i]);
        }
        if (res.msg) {
          this.echartsInit();
        }
      });
      // 运单统计
      transportMsg().then((res) => {
        this.transportInfo = res.data;
      });
      // 运输准时率
      rightMsg().then((res) => {
        if (res.data.departRate == 0 || res.data.departRate == "") {
          res.data.departRate = 0;
        }
        if (res.data.finishRate == 0 || res.data.finishRate == "") {
          res.data.finishRate = 0;
        }
        this.$set(this.punctuality, "departRate", res.data.departRate + "%");
        this.$set(this.punctuality, "finishRate", res.data.finishRate + "%");
        this.$set(this.anomaly, "exceptionCount", res.data.exceptionCount);
        this.$set(this.anomaly, "exceptionRate", res.data.exceptionRate);
        this.anomalyMsg = res.data.exceptionTasks;
        this.punctualityMsg = res.data.finishTasks;
      });
      // 线路图
      lineMsg().then((res) => {
        let a = res.data
        this.$set(a, "徐州短驳", 660);
       for (const j in a) {
          let index = j.indexOf("-");
          if (index == -1) {
            this.$delete(a, j);
          }
        }
         for (const i in res.data) {
          var carMessage = {};
          const carFrom = {};
          let newArray = [];
          let index = i.indexOf("-");
          carMessage["name"] = i.substring(0, index);
          carFrom["name"] = i.substring(index + 1);
          carMessage["value"] = res.data[i];
          newArray.push(carFrom, carMessage);
          this.YCData.push(newArray);
        }
       
        if (res.msg) {
          this.chinachart();
        }
      });
    },
    //目的地运输量
    echartsInit() {
      //柱形图
      //因为初始化echarts 的时候，需要指定的容器 id='main'
      this.$echarts.init(document.getElementById("mycharts")).setOption({
        xAxis: {
          type: "category",
          data: this.bournX,
          axisLabel: {
            interval: 0,
            rotate: -30,
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        yAxis: {
          type: "value",
        },

        series: [
          {
            data: this.bournY,
            type: "bar",
          },
        ],
      });
    },
    // 车辆装载
    echartsCar() {
      this.$echarts.init(document.getElementById("car")).setOption({
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: "50%",
            data: this.carMessage,
          },
        ],
      });
    },
    convertData(data) {
      var res = [];
      for (var i = 0; i < data.length; i++) {
        var dataItem = data[i];
        var fromCoord = geoCoordMap[dataItem[0].name];
        var toCoord = geoCoordMap[dataItem[1].name];
        if (fromCoord && toCoord) {
          res.push({
            fromName: dataItem[0].name,
            toName: dataItem[1].name,
            coords: [fromCoord, toCoord],
            value: dataItem[1].value,
          });
        }
      }
      return res;
    },
    // 中国地图
    chinachart() {
      let series = [];
      [["徐州", this.YCData]].forEach((item, index) => {
        series.push(
          {
            name: item[0] + " Top3",
            type: "lines",
            zlevel: 1,
            effect: {
              show: true,
              period: 6,
              trailLength: 0.7,
              color: "#fff", //arrow箭头的颜色
              // 运输点的大小
              symbolSize: 3,
            },
            lineStyle: {
              normal: {
                color: "#fff",
                width: 0,
                curveness: 0.2,
              },
            },
            data: this.convertData(item[1]),
          },
          {
            name: item[0] + " Top3",
            type: "lines",
            zlevel: 2,
            symbol: ["none", "arrow"],
            symbolSize: 10,
            effect: {
              show: true,
              period: 6,
              trailLength: 0,
              symbol: planePath,
              symbolSize: 15,
            },
            lineStyle: {
              normal: {
                color: "#fff",
                width: 1,
                opacity: 0.6,
                curveness: 0.2,
              },
            },
            data: this.convertData(item[1]),
          },
          {
            name: item[0] + " Top3",
            type: "effectScatter",
            coordinateSystem: "geo",
            zlevel: 2,
            rippleEffect: {
              brushType: "stroke",
            },
            label: {
              normal: {
                show: true,
                position: "left",
                formatter: "{b}",
              },
            },
            symbolSize: function (val) {
              return 10;
            },
            itemStyle: {
              normal: {
                color: "#fff",
              },
              emphasis: {
                areaColor: "#2B91B7",
              },
            },
            data: item[1].map(function (dataItem) {
              return {
                name: dataItem[1].name,
                value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value]),
              };
            }),
          }
        );
      });
      this.myEcharts = this.$echarts.init(this.$refs.mapBox);
      let option = {
        tooltip: {
          trigger: "item",
          formatter: function (params, ticket, callback) {
            if (params.seriesType == "effectScatter") {
              return "线路：" + params.data.name + "" + params.data.value[2];
            } else if (params.seriesType == "lines") {
              return (
                params.data.fromName +
                "-" +
                params.data.toName +
                "<br />" +
                params.data.value
              );
            } else {
              return params.name;
            }
          },
        },
        geo: {
          map: "china",
          roam: false,
          //   放大我们的地图
          zoom: 1.25,

          itemStyle: {
            normal: {
              areaColor: "#CF5143",
              borderColor: "#FBB392",
              borderWidth: 1,
            },
            emphasis: {
              areaColor: "#E78571",
            },
          },
        },
        series: series,
      };
      this.myEcharts.setOption(option);
    },
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}

.main {
  min-width: 1024px;
  max-width: 1920px;
  height: 100%;
  padding: 10px 10px 0;
  margin: 0 auto;
  background-color: #f3f4f7;
  display: flex;
}
.first {
  /* background-color: pink; */
  display: flex;
}

.first span {
  margin-right: 10px;
  display: block;
  width: 7px;
  height: 15px;
  background: linear-gradient(136deg, #db342d, #fe977e, 79%, #fdb892 99%);
}
.box {
  flex: 3;
  display: flex;
  flex-direction: column;
  /* background-color: red; */
}
.box1 {
  background-color: pink;
  // height: 10%;
}
.main .box:nth-child(2) {
  margin: 0px 0.125rem 0.1875rem;
  flex: 6;
}
.panel {
  width: 100%;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  height: 241px;
  padding: 10px;
  overflow: hidden;
  margin: 5px 0px;
}
.box1 {
  width: 100%;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  height: 180px;
  padding: 10px;
  overflow: hidden;
  margin: 5px 0px;
}
.on {
  display: flex;
  flex-direction: column;
  height: 250px;
  padding: 15px;
  /* background-color: blue; */
}
.on h4 {
  /* background-color: yellow; */
  height: 50px;
}
.map {
  /* background-color: red; */
  height: 500px;
  padding: 15px;
  text-align: center;
  display: flex;
  flex-direction: column;
}
.on_hd {
  flex: 1;
  /* background-color: red; */
  padding: 0px 10px;
}
.hd {
  display: flex;
  height: 80%;
  justify-content: space-between;
  /* background-color: green; */
  margin-top: 20px;
}
.hd li {
  width: 40%;
  background-color: #fff;
  height: 60%;
  margin: 10px 2px 0px;
  box-shadow: 10px 10px 10px #f8f8f9;
  display: flex;
  align-items: center;
  padding: 5px;
  border-radius: 5px;
}
.right {
  height: 100%;
  margin-left: 8px;
  /* background-color: green; */
  display: flex;
  width: 80px;
  flex-direction: column;
  justify-content: space-around;
}
#mycharts {
  margin-top: -20px;
}
.driver {
  width: 100%;
  height: 100%;
  padding: 20px;
  display: flex;
  justify-content: space-around;
}
.showDriver {
  text-align: center;
}
.number {
  margin-bottom: 5px;
  color: #99b5f7;
  font-weight: 700;
}
.left {
  /* background-color: green; */
  width: 50px;
  height: 50px;
}
.left img {
  width: 100%;
  height: 100%;
}
.arrive {
  /* background-color: pink; */
  width: 100%;
  table-layout: fixed;
}
.arrive tr {
  height: 30px;
  /* background-color: green; */
  text-align: center;
}
.arrive th {
  font-weight: 400;
  color: #b3b3b3;
}
.arrive td {
  text-overflow: ellipsis;
  -moz-text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.num {
  width: 150px;
}
#china_map {
  height: 100%;
}
.time {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: space-around;
  align-items: center;
  /* background-color: green; */
}
.time .img {
  /* background-color: yellow; */
  height: 30px;
  width: 40px;
  margin-left: 20px;
}
.time .img img {
  width: 100%;
  height: 100%;
}
.probability {
  /* background-color: red; */
  text-align: center;
}
.probability p:nth-child(1) {
  margin: 5px 0;
}
.yunshu {
  height: 170px;
}
.pane {
  height: 312px;
  display: flex;
  flex-direction: column;
  background-color: #f3f4f7;
  justify-content: space-between;
}
.daoda {
  height: 190px;
}
.yichang_top {
  padding: 5px 10px;
  height: 40%;
  background: linear-gradient(to right, #ce4e40, #da6957, #e78772);
  border-radius: 5px;
  color: #fff;
  margin-bottom: 10px;
}
.link {
  background-color: pink;
  /* height: 6%; */
  position: relative;
}
.link img:first-child {
  position: absolute;
  height: 49px;
  width: 13px;
  bottom: -19px;
  left: 75px;
}
.link img:last-child {
  position: absolute;
  height: 49px;
  width: 13px;
  bottom: -19px;
  right: 75px;
}
.yichang_bottom {
  padding: 9px 10px;
  padding-bottom: 0px;
  width: 100%;
  height: 26%;
  background-color: #fff;
}
.pane_top {
  margin-top: -17px;
  display: flex;
  justify-content: space-between;
  /* background-color: yellow; */
  align-items: center;
  padding: 10px;
}
.xuxian {
  text-align: center;
  width: 100%;
}
.yichang_left {
  /* background-color: pink; */
  width: 50%;
  text-align: center;
  height: 80px;
  font-weight: 700;
  font-size: 14px;
}
.yichang_left p:nth-child(1) {
  margin: 15px 0;
}
.yichang_right {
  /* background-color: red; */
  width: 40%;
  height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  margin-bottom: 10px;
}
.yichang_right p:nth-child(1) {
  margin: 15px 0;
}
.yichang_right p:nth-child(2) {
  /* background-color: green; */
  border: 1px solid #fff;
  width: 50px;
  height: 30px;
  border-radius: 25px;
  text-align: center;
  line-height: 30px;
  margin-top: -10px;
}
#mapBox {
  /* width: 1200px; */
  height: 100%;
}
.seamless-warp2 {
  background-color: #fff;
  height: 700px;
  overflow: hidden;
  ul li {
    display: flex;
    span {
      flex: 1;
      text-align: center;
      padding: 5px 0px;
    }
  }
}
</style>
